package com.ebupt.migu.music.propagate.job;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebupt.migu.music.propagate.mapper.PropagateEffectMapper;
import com.ebupt.migu.music.propagate.mapper.PropagateStrategyMapper;
import com.ebupt.migu.music.redis.service.RedisService;
import com.ebupt.migu.propagate.PropagateStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.Cursor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class PropagateEffect {

    //redis


    @Resource
    private PropagateEffectMapper propagateEffectMapper;
    @Resource
    private PropagateStrategyMapper propagateStrategyMapper;

    @Autowired
    private RedisService redisService;


    //3.添加定时任务
    @Scheduled(cron = " 0 0 23 * * ?")
    //@Scheduled(cron = " */5 * * * * ?")
    private void configureTasks() {
        String formattedDate = DateUtil.format(new Date(),"yyyy-MM-dd");
        String key="PROPAGATE_STRATEGY_"+formattedDate;
        //查询锁是否存在
        Boolean aBoolean = redisService.hasKey(key);
        if (!aBoolean) {
            //加锁
            redisService.add("key", formattedDate);
            //查询所有已完成策略
            List<PropagateStrategy> propagateStrategies = propagateStrategyMapper.selectList(new QueryWrapper<>());
            propagateStrategies.forEach(propagateStrategy -> {
                List list = propagateEffectMapper.queryById(propagateStrategy.getStrategyId());
                String today = DateUtil.format(new Date(),"yyyyMMdd");
                // 成功数
                String key_success = propagateStrategy.getStrategyId() + "_" + today + "_success";
                // 失败数
                String key_fail = propagateStrategy.getStrategyId() + "_" + today + "_fail";
                Object S = redisService.get(key_success);
                Object F = redisService.get(key_fail);
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("strategyId", propagateStrategy.getStrategyId());
                hashMap.put("strategyName", propagateStrategy.getStrategyName());
                hashMap.put("succeed", S==null?0:Integer.valueOf(S.toString()));
                hashMap.put("fail",  F==null?0:Integer.valueOf(F.toString()));
                hashMap.put("executeTime", propagateStrategy.getStartTime());
                hashMap.put("createTime", propagateStrategy.getCreateTime());
                hashMap.put("creatorId", propagateStrategy.getCreatorId());
                hashMap.put("creatorName", propagateStrategy.getCreatorName());
                hashMap.put("creatorDepartment", propagateStrategy.getCreatorDepartment());
                if (list.isEmpty()) {
                    propagateEffectMapper.addEffectRecords(hashMap);
                } else {
                    propagateEffectMapper.updateEffect(hashMap);
                }
                System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
            });
            redisService.remove(key);
        }
    }
}
